package com.crm.crm.controller;

import com.crm.crm.config.AliyunOss.OSSAliyunFileStorageService;
import com.crm.crm.domain.vo.business.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @ClassName CommonController
 * @Description 通用请求处理
 * @Author navel
 * @Date 2025/6/4 下午5:00
 * @Version 1.0
 */
@RestController
@RequestMapping("/common")
public class CommonController {

    @Autowired
    private OSSAliyunFileStorageService fileStorageService;

    @PostMapping("/upload")
    public Result uploadFile(MultipartFile file) throws Exception{
        try
        {
            // 上传文件路径
           /* String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;*/

            //文件名--->UUID.后缀
            String originalFilename = file.getOriginalFilename();//abc.jpg
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String filename = UUID.randomUUID().toString()+extension;

            //把文件上传到oss中
            String url = fileStorageService.store(filename, file.getInputStream());


            Result result = Result.success();
            result.put("url", url);
            result.put("fileName", url);
//            result.put("newFileName", FileUtils.getName(fileName));
            result.put("originalFilename", file.getOriginalFilename());
            return result;
        }
        catch (Exception e)
        {
            return Result.error(e.getMessage());
        }
    }
}
